package cc.messcat.web.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.constant.CommonRedisConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.entity.AreaVO;
import cc.messcat.entity.area.Area;
import cc.messcat.entity.fupin.PoorVillage;
import cc.messcat.entity.helpUnits.HelpTeam;
import cc.messcat.entity.helpUnits.HelpTeamPerson;
import cc.messcat.entity.system.McUser;
import cc.messcat.service.fupin.PoorVillageService;
import cc.messcat.service.helpTeam.HelpTeamService;
import cc.messcat.service.system.UserService;

@Controller
@RequestMapping("/helpTeam")
public class HelpTeamController {

    @Resource
    private HelpTeamService helpTeamService;

    @Autowired
    private UserService userService;
    
    @Autowired
    @Qualifier("poorVillageServiceImpl")
    private PoorVillageService poorVillageService;

    @RequestMapping(value = "addHelpTeamPerson", method = RequestMethod.POST)
    @ResponseBody
    public Object addHelpTeamPerson(HelpTeamPerson helpTeamPerson) {
        HelpTeam searchTeamCondition = helpTeamPerson.getTeam();
        HelpTeam team = helpTeamService.getTeamByCondition(searchTeamCondition);
        if (team != null)
            helpTeamPerson.setTeam(team);
        helpTeamService.addHelpTeamPerson(helpTeamPerson);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS);
    }

    @RequestMapping(value = "getHelpTeamPersonByCondition2", method = RequestMethod.POST)
    @ResponseBody
    @Deprecated
    public Object getHelpTeamPersonByCondition2(HelpTeamPerson helpTeamPerson, Integer pageNo, Integer pageSize) {
        Pager pager = this.helpTeamService.getPersonByVilliagesId(helpTeamPerson, pageNo, pageSize);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXECUTE_SUCCESS, pager);
    }

    @RequestMapping(value = "getHelpTeamPersonByCondition", method = RequestMethod.POST)
    @ResponseBody
    public Object getHelpTeamPersonByCondition(HelpTeamPerson helpTeamPerson, AreaVO areaVO, Integer pageNo, Integer pageSize,
    	HttpSession session) {
    	McUser user = userService.retrieveUserByUsername((String) session.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER));
    	Area area = null; 
        if (BeanUtil.isValid(helpTeamPerson) && BeanUtil.isValid(helpTeamPerson.getTeam()) && BeanUtil.isValid(helpTeamPerson.getTeam().getVillage()) && BeanUtil.isValid(helpTeamPerson.getTeam().getVillage().getId())) {
            area = poorVillageService.retrieveObject(Area.class, helpTeamPerson.getTeam().getVillage().getId());
            areaVO.setName(area.getArea());
        }
        Pager pager = this.helpTeamService.getPersonByCondition(user,area,helpTeamPerson, areaVO, pageNo, pageSize);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXECUTE_SUCCESS, pager);
    }

    @RequestMapping(value = "getHelpTeamPersonByVillage", method = RequestMethod.POST)
    @ResponseBody
    public Object getHelpTeamPersonByVillage(HelpTeamPerson helpTeamPerson, PoorVillage poorVillage, Integer pageNo, Integer pageSize) {
        List<Long> list = new ArrayList<>();
        List<PoorVillage> villages = poorVillageService.getVillagesByCondition(poorVillage);
        for (PoorVillage village : villages)
            list.add(village.getId());
        Pager pager = this.helpTeamService.getPersonByVilliagesId(helpTeamPerson, list.toArray(new Long[list.size()]), pageNo, pageSize);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXECUTE_SUCCESS, pager);
    }

    @RequestMapping(value = "removeHelpTeamPersonById")
    @ResponseBody
    public Object removeHelpTeamPersonById(Long id) {
        helpTeamService.removeHelpTeamPersonById(id);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXECUTE_SUCCESS);
    }
    
    /**
     * 以贫困村id查询工作队的人员
     * @param id
     * @return
     */
    @RequestMapping(value = "getHelpTeamPersonByPoorVillage")
    @ResponseBody
    public Object getHelpTeamByPoorVillage(@RequestParam(name="id",required=true)Long id) {
    	List<Map<String,Object>> list = helpTeamService.getHelpTeamPersonByPoorVillage(id);
    	return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,list);
    }

}
